package polsense.filemerger;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.Checksum;

/* CRC.java
 * Created on Oct 12, 2005
 * Copyright 2005 Pablo J. Lopez
 */

/**
 * @author Pablo J. Lopez
 */
public class MP3CRC {
	


	  public static long getChecksumValue(Checksum checksum, File file,  int bufferSize) throws IOException {
	     
		  RandomAccessFile raf = null;
		  FileChannel channel = null;
		  ByteBuffer bytebuff1 = ByteBuffer.allocate(bufferSize);
	
		  try {
				
				
					// We check in the middle where there is no ID3 encoded data (hopefully) 
					
					long middle = file.length() / 2;
					
					raf = new RandomAccessFile(file,"r");
					raf.seek(middle);
					channel = raf.getChannel();
					
					channel.read(bytebuff1);
					
					checksum.update(bytebuff1.array(), 0, bufferSize);
					
					
					return checksum.getValue();
		         
	        }finally {
				
				try {
					channel.close();
					raf.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
	        }
			
		
	    }

	 
	}

